<template>
  <div >
    <view>
      <van-toast id="van-toast" />
      <van-button type="info"  size="small" @tap="getToXls('all')" >全部导出</van-button>
      <van-button type="info"  size="small" @tap="getToXls('today')" >导出当前时间至当天0点的所有记录</van-button>
      <van-button type="info"  size="small" @tap="getToXls('yeday')" >导出当前时间至前一天0点的所有记录</van-button>
      <van-button type="info"  size="small" @tap="getToXls_date()">导出昨天({{yesday}})订单</van-button>
      <van-button type="info"  size="small" @tap="zhiding_show = true" >选择单个日期 {{ zhiding_date }}</van-button>
      <van-button type="info"  size="small" @tap="qujian_show = true" >选择区间日期 {{ qujian_date }}</van-button>

      <van-calendar
        :default-date="dday"
        :min-date="minDate"
        :max-date="maxDate"
        :show="zhiding_show"
        @close="onClose_zhiding_show"
        @confirm="zhiding_onConfirm"
      />



      <van-calendar
        :default-date="ddays"
        :min-date="minDate"
        :max-date="maxDate"
        :show="qujian_show"
        type="range"
        @close="onClose_qujian_show"
        @confirm="qujian_onConfirm"
      />



      <!--<van-button type="info"  size="small" @tap="getToXls(4)" >导出指定日期</van-button>-->
      <!--<van-button type="info"  size="small" @tap="getToXls(5)" >导出指定条数</van-button>-->
      <!--<van-button type="info"  size="small" @tap="getToXls('sum')" >导出上次导出之后的所有新数据（上次导出是20222）</van-button>-->

      <div v-if="isUrl">
        <van-button type="primary"  size="small" @tap="copy" >复制下载链接{{firename}}</van-button>
      </div>


    </view>
  </div>
</template>

<script>

  import {orderToXls} from '../../api/basisMG'

  import Toast from  '../../../static/vant/toast/toast';


  import * as dayjs from 'dayjs'
  import 'dayjs/locale/zh-cn' // import locale



  export default {
    data () {
      return {
        zhiding_show:false,
        qujian_show:false,
        zhiding_date:"",
        qujian_date:"",
        isUrl:false,
        firename:"",
        urldown:"",
        orderlist:[],
        tiaoshu:0,
        minDate: new Date().getTime()-7776000000,//90天
        maxDate: new Date().getTime(),
        dday:new Date().getTime(),
        ddays:[new Date().getTime()-577600000,new Date().getTime()],
        yesday:""
      }
    },

    mounted() {
      this.isUrl = false

     // this.minDate = new Date(2021, 2, 1)
     // this.maxDate = new Date(2021, 6, 1)


      var day1 = new Date();
      day1.setTime(day1.getTime()-24*60*60*1000);
      var s1 = day1.getFullYear()+"-" + (day1.getMonth()+1) + "-" + day1.getDate();
      this.yesday =  s1


      //this.yesday =  Date.parse(day1)


      //this.tiaoshufun()
    },

    methods:{
      onClose_qujian_show(){
        this.qujian_show = false
      },
      onClose_zhiding_show(){
        this.zhiding_show = false
      },
      formatDate(date) {
        return `${date.getMonth() + 1}/${date.getDate()}`;
      },
      zhiding_onConfirm(date){
        let t =dayjs(date.mp.detail).format('YYYY-MM-DD')
        this.zhiding_show = false;
        this.zhiding_date = t
        this.getToXls_date(t)
      },
      qujian_onConfirm(date) {
        let start = dayjs(date.target[0]).format('YYYY-MM-DD')
        let end =  dayjs(date.target[1]).format('YYYY-MM-DD')
        this.qujian_show = false;
        this.qujian_date = start +"-"+ end
        this.getToXls_date([start,end])
      },
      copy(){
        wx.setClipboardData({
          //准备复制的数据
          data: this.urldown,
          success: function (res) {
            Toast.success({
              message: '复制完成'
            });
          }
        })
      },

      getToXls(type){
        orderToXls({"type":type}).then(res=>{
          this.firename = res.url
          this.isUrl = true
          this.urldown = "http://39.101.138.78:25811/xlsx/"+res.url+".xlsx"
        })
      },
      getToXls_date(date){
        orderToXls({"type":"date","date":date}).then(res=>{
          this.firename = res.url
          this.isUrl = true
          this.urldown = "http://39.101.138.78:25811/xlsx/"+res.url+".xlsx"
        })
      }

    }
  }
</script>

<style >
  van-button {margin: 32rpx; margin-bottom: 44px; line-height: 44px;}


</style>
